package example.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * 创建 队列 配置类
 */
@Configuration
public class TtlQueueConfig {


    // 普通交换机的名称
    public static final String X_EXCHANGE = "X";
    // 死信交换机名称
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";

    // 普通队列的名称
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    public static final String QUEUE_C = "QC";

    // 死信队列的名称
    public static final String DEAD_LETTER_QUEUE = "QD";

    // 声明X交换机
    @Bean(name = "xExchange")
    public DirectExchange xExchage(){
        return new DirectExchange(X_EXCHANGE);
    }

    // 声明X交换机
    @Bean(name ="yExchange")
    public DirectExchange yExchage(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }


    // 申明普通队列 QA TTL为10s
    @Bean(name ="queueA")
    public Queue queueA(){
        Map arguments = new HashMap();
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        // 设置死信routingkey
        arguments.put("x-dead-letter-routing-key", "YD");
        // 设置ttl过期时间 ms
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    // 申明普通队列 QB TTL为40s
    @Bean(name ="queueB")
    public Queue queueB(){
        Map arguments = new HashMap();
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        // 设置死信routingkey
        arguments.put("x-dead-letter-routing-key", "YD");
        // 设置ttl过期时间 ms
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    // 申明普通队列 QC 没有延迟
    @Bean(name ="queueC")
    public Queue queueC(){
        Map arguments = new HashMap();
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        // 设置死信routingkey
        arguments.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }

    // 申明死信队列 QB TTL为40s
    @Bean(name ="queueD")
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    // 绑定 消息队列A绑定xExchange
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    // 绑定 消息队列B绑定xExchange
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    // 绑定 消息队列C绑定xExchange
    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }

    // 绑定 消息队列D绑定xExchange
    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
}
